import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.*;
import java.util.Scanner;

public class JDBCupdate {
    public static void main(String[] args) throws SQLException {
        //1.创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("101.42.170.247lse");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");

        //2.建立连接
        Connection connection = dataSource.getConnection();

        //3.输入要修改的id
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的id：");
        int id = scanner.nextInt();
        System.out.println("请输入要修改后的名字：");
        String name = scanner.next();

        //4.拼装sql语句
        String sql = "update student set name = ? where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        statement.setInt(2,id);
        System.out.println("statement " + statement);

        //5.执行sql语句
        int ret = statement.executeUpdate();
        System.out.println(ret);

        //6.回收释放资源
        statement.close();
        connection.close();
    }
}
